package cn.tedu.v1.service.impl;

import cn.tedu.v1.model.DepartmentOffice;
import cn.tedu.v1.mapper.DepartmentOfficeMapper;
import cn.tedu.v1.service.IDepartmentOfficeService;
import cn.tedu.v1.vo.ChartVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 部门列表 服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-06
 */
@Service
@Slf4j
public class DepartmentOfficeServiceImpl extends ServiceImpl<DepartmentOfficeMapper, DepartmentOffice> implements IDepartmentOfficeService {

    @Resource
    DepartmentOfficeMapper departmentOfficeMapper;

    ChartVo chartVo = new ChartVo();

    @Override
    public List<DepartmentOffice> getDepartmentOffice() {
        QueryWrapper<DepartmentOffice> query = new QueryWrapper<>();
        List<DepartmentOffice> list = departmentOfficeMapper.selectList(query);
        log.debug("部门数量:{}", list.size());
        return list;
    }

    @Override
    public List<DepartmentOffice> treatmentDepartment() {
        List<DepartmentOffice> treatmentDepartment=departmentOfficeMapper.selectDepartmentOfficeList();
        return treatmentDepartment;
    }

    @Override
    public List<DepartmentOffice> medicalDepartment() {

        List<DepartmentOffice> medicalDepartment=departmentOfficeMapper.medicalDepartmentOfficeList();
        return medicalDepartment;
    }

    @Override
    public Integer deleteDepartmentOffice(Integer id) {
        int num = departmentOfficeMapper.updateDeleteStatusById(id);
        return num;
    }

    @Override
    //查询部门人数
    public ChartVo getDepartmentCount() {
        Integer[] departmentId = departmentOfficeMapper.getDepartmentQuantity();
        Map<Integer,Integer> map = new HashMap<>();
        for(int id:departmentId){
            int count = departmentOfficeMapper.getDepartmentPeopleQuantity(id);
            System.out.println(count+"+++++"+id);
            map.put(id,count);
            log.debug(id+"号部门人数:{}"+count);
        }
        chartVo.setDepartmentPeopleQuantity(map);
        return chartVo;
    }

    @Override
    //查询部门名称
    public ChartVo getDepartmentName() {
        String[] name = departmentOfficeMapper.getDepartmentName();
        chartVo.setDepartmentName(name);
        return chartVo;
    }

    @Override
    //生成图标颜色
    public ChartVo getDepartmentColor() {
        String[] name = departmentOfficeMapper.getDepartmentName();
        String[] color = new String[name.length];
        for(int i = 0;i<name.length;i++){
            StringBuffer result = new StringBuffer();
            for(int j=0;j<6;j++) {
                //随机生成0-15的数值并转换成16进制
                result.append(Integer.toHexString(new Random().nextInt(16)));
            }
             color[i] = "#"+result.toString().toUpperCase();
        }
        chartVo.setDepartmentColor(color);
        return chartVo;
    }

    @Override
    public int addDepartment(DepartmentOffice departmentOffice) {
        DepartmentOffice add = new DepartmentOffice();
        //先查询部门是否存在
        Integer id = departmentOfficeMapper.selectDepartmentIdByDepartmentName(departmentOffice.getName());
        //查询科室id最大值
        int maxOfficeId = departmentOfficeMapper.selectOfficeIdMax();
        if(id == null){
            //不存在则查询部门id最大值
            int max = departmentOfficeMapper.selectDepartmentIdMax();
            //在新增部门
            add.setName(departmentOffice.getName())
                    .setDeleteStatus(0)
                    .setStatus(departmentOffice.getStatus())
                    .setChairman(departmentOffice.getChairman())
                    .setOfficeName(departmentOffice.getOfficeName())
                    .setDepartmentId(max+1)
                    .setOfficeId(maxOfficeId+1);
            int num = departmentOfficeMapper.insert(add);
            if(num == 1){
                return 1;
            }
            return 0;
        }else { //根据部门id查询部门名称
            String name = departmentOfficeMapper.selectNameById(id);
            //如果部门存在
            add.setName(name)
                    .setStatus(departmentOffice.getStatus())
                    .setChairman(departmentOffice.getChairman())
                    .setOfficeName(departmentOffice.getOfficeName())
                    .setOfficeId(maxOfficeId+1)
                    .setDepartmentId(id)
                    .setDeleteStatus(0);
            int num = departmentOfficeMapper.insert(add);
            if(num == 1){
                return 1;
            }else{
                return 0;
            }

        }

    }

}
